﻿using System;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    [Serializable]
    public class WenListViewItem : ListViewItem
    {
        public WenListView WenListView = null;
        public WenListViewItem() : base()
        {
        }
        public WenListViewItem(WenListView owner) : base()
        {
            this.WenListView = owner;
        }

        protected WenListViewItem(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) : base(serializationInfo, streamingContext)
        {
        }

        private WenListViewItemCellCollection cells;

        public WenListViewItemCellCollection Cells => cells ??= new WenListViewItemCellCollection(this);

        public class WenListViewItemCell
        {
            public WenListViewItemCell()
            {
            }

            public WenListViewItemCell(string name, object value)
            {
                Name = name;
                Value = value;
            }

            public string Name { get; set; }
            public object Value { get; set; }
        }
        public class WenListViewItemCellCollection : WenCollection
        {
            private readonly WenListViewItem owner;
            public WenListViewItemCellCollection(WenListViewItem owner)
            {
                this.owner = owner;
            }

            public void Add(string name, object value)
            {
                Items.Add(new WenListViewItemCell(name, value));
            }

            //返回值
            public WenListViewItemCell this[int index]
            {
                get => Items.Count <= index ? null : (WenListViewItemCell)Items[index];
                set
                {
                    if (Items.Count <= index)
                        return;

                    Items[index] = value;
                    ReWenListViewItemValue(index, value.Value?.ToString());
                }
            }
            public WenListViewItemCell this[string key]
            {
                get
                {
                    foreach (var item in Items)
                    {
                        if (item is WenListViewItemCell cell)
                        {
                            if (cell.Name.ToUpper() == key.ToUpper())
                            {
                                return cell;
                            }
                        }
                    }
                    return null;
                }
                set
                {
                    if (owner.WenListView.Columns[key] is WenColumnHeader ch)
                    {
                        int index = owner.WenListView.Columns.IndexOf(ch);
                        ReWenListViewItemValue(index, value.Value?.ToString());
                    }
                }
            }

            //设置值
            private void ReWenListViewItemValue(int index, string value)
            {
                Items[index] = value;
                if (index == 0)
                {
                    owner.Text = value;
                }
                else
                {
                    owner.SubItems[index - 1].Text = value;
                }
            }
        }
    }
}
